﻿using System;
using System.Collections;
using System.Linq;

namespace Framework.IISUtility
{
    /// <summary>
    /// IISWebServerCollection
    /// </summary>
    public class IISWebServerCollection : CollectionBase
    {
        /// <summary>
        ///
        /// </summary>
        public IISWebServer this[int Index]
        {
            get
            {
                return (IISWebServer)List[Index];
            }
        }
        /// <summary>
        ///
        /// </summary>
        public IISWebServer this[string ServerComment]
        {
            get
            {
                ServerComment = ServerComment.ToLower().Trim();
                return List.Cast<IISWebServer>().FirstOrDefault(list => list.ServerComment.ToLower().Trim() == ServerComment);
            }
        }
        internal void Add_(IISWebServer WebServer)
        {
            List.Add(WebServer);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebServer"></param>
        public void Add(IISWebServer WebServer)
        {
            try
            {
                List.Add(WebServer);
                IISManagement.CreateIISWebServer(WebServer);
            }
            catch
            {
                throw (new Exception("发生意外错误，可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }

        }
        /// <summary>
        /// 是否包含指定的网站
        /// </summary>
        /// <param name="ServerComment"></param>
        /// <returns></returns>
        public bool Contains(string ServerComment)
        {
            ServerComment = ServerComment.ToLower().Trim();
            for (int i = 0; i < List.Count; i++)
            {
                IISWebServer server = this[i];
                if (server.ServerComment.ToLower().Trim() == ServerComment)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// 是否包含指定的网站
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Contains(int index)
        {
            for (int i = 0; i < List.Count; i++)
            {
                IISWebServer server = this[i];
                if (server.index == index)
                    return true;
            }
            return false;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebServers"></param>
        public void AddRange(IISWebServer[] WebServers)
        {
            for (int i = 0; i <= WebServers.GetUpperBound(0); i++)
            {
                Add(WebServers[i]);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebServer"></param>
        public void Remove(IISWebServer WebServer)
        {
            for (int i = 0; i < List.Count; i++)
            {
                if (List[i] == WebServer)
                {
                    List.RemoveAt(i);
                    return;
                }
            }
            IISManagement.RemoveIISWebServer(WebServer.index);
        }
    }

}
